<!DOCTYPE html>

<html class="x-admin-sm" xmlns:th="http://www.thymeleaf.org">
  
  <head>
    <meta charset="UTF-8">
    <title>欢迎页面-X-admin2.1</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
    <link rel="stylesheet" href="lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="css/public.css" media="all">
  </head>
  <body>
		
    <div class="layui-fluid">

            <div class="layui-col-md9">
                <div class="x-body">
                        <!--表格-->
                    <table id="demo" lay-filter="test"></table>
                </div>
            </div>

            <div class="layui-col-md3">
                <fieldset class="layui-elem-field layui-field-title" style="margin-top: 30px;">
                    <legend>角色菜单关系表</legend>
                </fieldset>
                <div id="test4" class="demo-tree"></div>
            </div>
    </div>


    <!--toolbar :头部工具栏-->
    <script type="text/html" id="toolbarDemo">
      <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="add">新增</button>
      </div>
    </script>

    <!--tool :右侧工具栏-->
    <script type="text/html" id="barDemo">
      <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
<!--      <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>-->
    </script>

    <!--开关-->
    <script type="text/html" id="switchTpl">
      <input type="checkbox" name="status" value="{{d.id}}" lay-skin="switch" lay-text="启用|未启用" lay-filter="sexDemo" {{ d.status == 1 ? 'checked' : '' }}>
    </script>

	<!-- 时间模板 -->
    <script type="text/html" id="dateTpl">
        {{layui.util.toDateString(d.BirthDate, "yyyy-MM-dd") }}
    </script>

<script src="lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script src="js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script src="api/baseApi.js" charset="utf-8"></script>
<script th:inline="none">
      layui.use(['laydate','table','form','tree'], function(){
        var laydate = layui.laydate;
        var table = layui.table;
        var form = layui.form;
		var tree = layui.tree;
		var $ = layui.$;


        //执行一个laydate实例
        laydate.render({
          elem: '#start' //指定元素
        });

        //执行一个laydate实例
        laydate.render({
          elem: '#end' //指定元素
        });

        //第一个实例
        table.render({
          elem: '#demo'
          ,width:800
          ,cellMinWidth: 80 //宽度自适应
          ,toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
          ,defaultToolbar: ['filter', 'exports', 'print']//自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
          ,url: baseUrl + roleBaseUrl + 'pageRoleList' //数据接口
          ,page: true //开启分页
          ,limit: 10
          ,limits: [10,20,30]
          ,cols: [[ //表头
            {type: 'checkbox', fixed: 'left' }
            ,{field: 'id', title: 'ID', width:50, sort: true, fixed: 'left'}
            ,{field: 'rolename', title: '角色名称', width:100 }
            ,{field: 'createTime', title: '创建时间', width:160 },
			,{field: 'updateTime', title: '修改时间', width:160 }
            ,{field: 'status', title: '状态',templet: '#switchTpl', width:120}
            ,{fixed: 'right', align:'center', toolbar: '#barDemo', width:170}
          ]],
            text: {
                none: '暂无相关数据或权限不够'
            }
        });
				
        //手风琴模式
        tree.render({
            elem: '#test4'
            ,data: [{
                title: '优秀'
                ,children: [{
                    title: '80 ~ 90'
                },{
                    title: '90 ~ 100'
                }]
            },{
                title: '良好'
                ,children: [{
                    title: '70 ~ 80'
                },{
                    title: '60 ~ 70'
                }]
            },{
                title: '要努力奥'
                ,children: [{
                    title: '0 ~ 60'
                }]
            }]
            ,accordion: true
        });


        //头工具栏事件
        table.on('toolbar(test)', function(obj){
          var checkStatus = table.checkStatus(obj.config.id);
          switch(obj.event){
            case 'add':
              var data = checkStatus.data;
              //layer.alert(JSON.stringify(data));
              let index = layer.open({
                title:'添加角色',
                type:2,
                shade:0.2,
                maxmin:true,
                shadeClose:true,
                area:['100%','100%'],
                content:'roleAdd',
                yes: function(index, layero){
                  parent.location.reload(); // 父页面刷新
                  var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                  parent.layer.close(index);
                }
              })
              break;
            case 'batchDel':
              var data = checkStatus.data;
              layer.msg('选中了：'+ data.length + ' 个');
              break;
                  //自定义头工具栏右侧图标 - 提示
            case 'LAYTABLE_TIPS':
              layer.alert('这是工具栏右侧自定义的一个图标按钮');
              break;
          };
        });

        //监听状态开关操作
        form.on('switch(sexDemo)', function(obj){
          layer.tips(this.value + ' ' + this.name + '：'+ obj.elem.checked, obj.othis);
          var id = this.value; // 当行ID
          var status = this.checked ? '1' : '0'; // 选中状态
          var data ={id,status}
          console.log(data)
          $.ajax({
            url: baseUrl + roleBaseUrl ,
            type:'put',
            dataType: "json",
            contentType: "application/json;charset=UTF-8",
            data: JSON.stringify(data),
            success:function (resp) {
              layer.msg(resp.msg)
            }

          })
        });

        //监听行工具事件
        table.on('tool(test)', function(obj){
          var data = obj.data;
          //console.log(obj)
          if(obj.event === 'del'){
            layer.confirm('真的删除行么?', function(index){
              obj.del();
              $.ajax({
                url: baseUrl + roleBaseUrl + obj.data.id,
                type:'delete',
                success:function(res){
                  layer.msg(res.msg)
                  table.reload('demo')
                },
                error:function(res){
                  layer.msg(res.msg)
                }
              })
              layer.close(index);
            });
          } else if(obj.event === 'edit'){
            //将数据存入本地sessionData中
            layui.sessionData('roleData',{
              key:'role'
              ,value:data
            })
            let index = layer.open({
              title:'角色编辑',
              type:2,
              shade:0.2,
              maxmin:true,
              shadeClose:true,
              area:['100%','100%'],
              content:'roleEdit',
              yes: function(index, layero){
                parent.location.reload(); // 父页面刷新
                var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                parent.layer.close(index);
              }
            })
          }
        });
      });
</script>
<script>var _hmt = _hmt || []; (function() {
    var hm = document.createElement("script");
    hm.src = "https://hm.baidu.com/hm.js?b393d153aeb26b46e9431fabaf0f6190";
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(hm, s);
  })();
</script>
</body>
</html>